package com.example.administrator.kaohedemo.Store;

import com.example.administrator.kaohedemo.Action.Action;
import com.example.administrator.kaohedemo.Action.Actions;
import com.example.administrator.kaohedemo.Model.CourseListBean;
import com.google.gson.Gson;
import com.squareup.otto.Subscribe;

/**
 * Created by Administrator on 2016/6/21.
 */
public class CourseListStore extends Store{

    private static CourseListStore courseListStore;
    private static CourseListStoreChanged courseListStoreChanged;

    public static CourseListStore getInstense(){
        if (courseListStore==null){
            courseListStore=new CourseListStore();
        }
        return courseListStore;
    }

    @Override
    @Subscribe
    public void onAction(Action action) {

        GetCourseListStoreChanged();

        String type=action.getType();
        switch (type){
            case Actions.CourseListActions.TYPE_POP_ONCLICK:
                courseListStoreChanged.setType(type);
                courseListStoreChanged.setId((String) action.getMap().get(Actions.CourseListActions.KEY_ITEM_SELECT));
                UIChanged();
                break;
            case Actions.CourseListActions.TYPE_ITEM_ONCLICK:
            case Actions.CourseListActions.TYPE_TAGER_ONCLICK:
                courseListStoreChanged.setType(type);
                courseListStoreChanged.setPosition((Integer) action.getMap().get(Actions.CourseListActions.KEY_ITEM_SELECT));
                UIChanged();
                break;
            case Actions.CourseListActions.TYPE_POP_DISMISS:
            case Actions.CourseListActions.TYPE_TAGER_DESTORYONCLICK:
                courseListStoreChanged.setType(type);
                UIChanged();
                break;
            case Actions.CourseListActions.TYPE_HTTP_RESULT:
                courseListStoreChanged.setType(type);
                Gson gson=new Gson();
                String result=(String) action.getMap().get(Actions.CourseListActions.KEY_HTTP_RESULT);
                CourseListBean bean=gson.fromJson(result,CourseListBean.class);
                courseListStoreChanged.setBean(bean);
                UIChanged();
                break;
        }
    }

    private void GetCourseListStoreChanged() {
        if (courseListStoreChanged==null){
            courseListStoreChanged=new CourseListStoreChanged();
        }
    }

    @Override
    protected StoreChanged changed() {
        return courseListStoreChanged;
    }

    public class CourseListStoreChanged implements Store.StoreChanged{
        String type;
        int position;
        String id;
        CourseListBean bean;

        public void setType(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public CourseListBean getBean() {
            return bean;
        }

        public void setBean(CourseListBean bean) {
            this.bean = bean;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }
}
